package com.qk.controller;

import com.qk.common.utils.AliyunOSSOperator;
import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j
public class UploadController {
    private final AliyunOSSOperator aliyunOSSOperator;

    public UploadController(AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }

    /*  @PostMapping("/upload")
        public Result upload(String username,Integer age, MultipartFile file) throws Exception{
                log.info("{},{},{}",username,age,file);
                //1.构造文件名（不能重复） - > 使用uuid
                String fileName =  file.getOriginalFilename();
                String extName = fileName.substring(fileName.lastIndexOf("."));//substring
                String newFileName = UUID.randomUUID().toString() + extName;
                //2.将文件保存到磁盘中-- >指定保存到那个文件中
                file.transferTo(new File("D:\\develop\\upload\\" + newFileName ));
                return Result.success();
        }*/
    @PostMapping("/upload")
    public Result upload( MultipartFile image) throws Exception{
        log.info("文件上传：{}",image);
        String fileName =  image.getOriginalFilename();
        String extName = fileName.substring(fileName.lastIndexOf("."));//substring
        String newFileName = UUID.randomUUID().toString() + extName;
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }
}
